<?php
require_once("classes/db.class.php");
include("mysql.config.php");
session_start();
$db = new DataBase($db_address, $db_username, $db_password, $db_db);

$uzkl = "SELECT * FROM `works` WHERE '1'='1'";
$t = $db->query($uzkl);
$j = 0;
while($data = $db->fetch($t)){
    $wid = $data->id;
    $uzkl = "SELECT * FROM `feedbacks` WHERE `workid`='$wid'";
    $t2 = $db->query($uzkl);
    $i = 0;
    $sum = 0;
    while ($data2 = $db->fetch($t2)){
        $sum = $sum + $data2->speed + $data2->quality;
        $i++;
    }
    if ($sum != 0) {
        $vidurkiai[$j] = array(($sum / ($i * 2)), $data->id, $data->description, $data->uid);
        $j++;
    }
}
sort($vidurkiai);
if ($j > 5) {
    $geriausias = $vidurkiai[$j-1];
    if ($_SESSION['uid'] == $geriausias[3])
        echo '<div class="alert-message success"><p>Your work "' . $geriausias[2] .'" highest rated in company</p></div>';
    $geriausias = $vidurkiai[$j-2];
    if ($_SESSION['uid'] == $geriausias[3])
        echo '<div class="alert-message success"><p>Your work "' . $geriausias[2] .'" highest rated in company</p></div>';
    $geriausias = $vidurkiai[$j-3];
    if ($_SESSION['uid'] == $geriausias[3])
        echo '<div class="alert-message success"><p>Your work "' . $geriausias[2] .'" highest rated in company</p></div>';
    $geriausias = $vidurkiai[$j-4];
    if ($_SESSION['uid'] == $geriausias[3])
        echo '<div class="alert-message success"><p>Your work "' . $geriausias[2] .'" highest rated in company</p></div>';
} else {
    $geriausias = $vidurkiai[$j-1];
    if ($_SESSION['uid'] == $geriausias[3])
        echo '<div class="alert-message success"><p>Your work "' . $geriausias[2] .'" highest rated in company</p></div>';
}
?>
